package cn.zhangxin.pethome.system.role.service.impl;

import cn.zhangxin.pethome.base.query.BaseQuery;
import cn.zhangxin.pethome.system.employee.domain.Employee;
import cn.zhangxin.pethome.system.logininfo.domain.Logininfo;
import cn.zhangxin.pethome.system.menu.domain.Menu;
import cn.zhangxin.pethome.system.menu.mapper.MenuMapper;
import cn.zhangxin.pethome.system.permission.domain.Permission;
import cn.zhangxin.pethome.system.permission.mapper.PermissionMapper;
import cn.zhangxin.pethome.system.role.domain.Role;
import cn.zhangxin.pethome.system.role.domain.RoleMenu;
import cn.zhangxin.pethome.system.role.domain.RolePermission;
import cn.zhangxin.pethome.system.role.mapper.RoleMapper;
import cn.zhangxin.pethome.system.role.service.IRoleService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import cn.zhangxin.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-02-02
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    //添加
    @Override
    public void add(Role role) {
        //先添加获得role的id
        roleMapper.add(role);
        addPermissAndMenu(role);

    }
    //修改
    @Override
    public void update(Role role) {
        if (role.getId() == null){
            throw new BusinessException("非法访问");
        }
        //总体逻辑 先删后加（先删除关联表数据）
        roleMapper.deleteMenuByRoleId(role.getId());
        roleMapper.deletePermissionByRoleId(role.getId());
        addPermissAndMenu(role);
        super.update(role);
    }
    //分页查询
    @Override
    public List<Role> findByQuery(BaseQuery baseQuery) {
        //分页查询
        List<Role> roles = roleMapper.findByQuery(baseQuery);
        //通过roleId获得菜单对象和权限对象
        for (Role role : roles){
            //添加菜单数据
            List<Menu> menu = menuMapper.findAllByRoleId(role.getId());
            role.setMenuList(menu);
            //添加权限数据
            List<Permission> permissioner = permissionMapper.findAllByRoleId(role.getId());
            role.setPermissionList(permissioner);
        }
        return roles;
    }
    //删除
    @Override
    public void delete(Long id) {
        //先删除中间表，再删除主表
        roleMapper.deletePermissionByRoleId(id);
        roleMapper.deleteMenuByRoleId(id);
        super.delete(id);
    }

    //公共代码
    private void addPermissAndMenu(Role role) {
        //因为事务 所以判断可以提取到方法中来
        if (role.getName()==null || "".equals(role.getName().trim())){
            throw new BusinessException("角色名称不能为空");
        }
        if (role.getSn()==null || "".equals(role.getSn().trim())){
            throw new BusinessException("角色编号不能为空");
        }
        if (role.getMenus().isEmpty()){
            throw new BusinessException("角色菜单不能为空");
        }
        if (role.getPermissions().isEmpty()){
            throw new BusinessException("角色权限不能为空");
        }
        //补充角色权限中间表
        //获得权限id集合
        List<Long> permissionIds = role.getPermissions();
        //新建一个角色权限中间表对象
        ArrayList<RolePermission> RolePermissions = new ArrayList<>();
        //循环添加权限中间表
        for (Long m :permissionIds){
            RolePermission rolePermission = new RolePermission(role.getId(),m);
            RolePermissions.add(rolePermission);
        }
        roleMapper.rolePermissionBatchInsert(RolePermissions);

        //补充权限菜单中间表
        List<Long> menuIds = role.getMenus();
        //新建一个角色菜单中间表对象
        ArrayList<RoleMenu> roleMenus = new ArrayList<>();
        //循环添加数据
        for (Long menuId: menuIds){
            RoleMenu roleMenu = new RoleMenu(role.getId(),menuId);
            roleMenus.add(roleMenu);
        }
        //批量新增
        roleMapper.roleMenuBatchInsert(roleMenus);
    }


}
